-
Notifications
You must be signed in to change notification settings - Fork 3k
sockstat: Add automatic column sizing and remove -w option #1720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Thank you for taking the time to contribute to FreeBSD!
|
The automatic column sizing works pretty well. But it isn't perfect. On my machine, with almost any options, sockets whose protocol is "stream (not connected)" will have misaligned columns. This is easiest to see with the "-l" option. It looks like the "(not connected)" part isn't contributing to the calculation of the width of the PROTO column. For example:
Also, there's another change that wasn't mentioned in the commit message. Is this deliberate?
|
Thanks, @asomers, You're absolutely right, Regarding the |
Using "??" uniformly is ok. The only risk is that it upsets some scripts that people use to parse sockstat's output with awk. But those scripts are fragile anyway. Using libxo would be much more reliable! |
usr.bin/sockstat/sockstat.c
Outdated
printf(" %-*s", cw->local_addr, "(not connected)"); | ||
strcpy(buf,"??"); | ||
if (laddr->address.ss_len > 0) | ||
formataddr(&laddr->address, buf, sizeof(buf)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line will overwrite the "??" from line 1399. If you meant for line 1399 to be more like a default initializer, I suggest you move it below, with else
. There are similar patterns elsewhere, like on line 1407-1409.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed!
usr.bin/sockstat/sockstat.c
Outdated
@@ -1344,33 +1524,38 @@ display(void) | |||
struct passwd *pwd; | |||
struct file *xf; | |||
struct sock *s; | |||
int n, pos; | |||
int n; | |||
struct col_widths cw; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some spaces snuck in on these two lines. You should consistently indent with tabs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed!
usr.bin/sockstat/sockstat.c
Outdated
static void | ||
calculate_column_widths(struct col_widths *cw) | ||
{ | ||
cw->user = 4; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here you indented with spaces. Please change them all to tabs. See style(9) for a detailed description of how to format C code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed! Thanks for the reference.
usr.bin/sockstat/sockstat.c
Outdated
cw->stack = TCP_FUNCTION_NAME_LEN_MAX; | ||
cw->cc = TCP_CA_NAME_MAX; | ||
|
||
char buf[512]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a lot of memory to put on the stack. Better to use malloc(). But in this case, you don't have to! It's possible to eliminate the need for buf entirely, but taking advantage of the fact that snprintf
always returns the number of characters that it would've written if the buffer had been big enough. So just call len = snprintf(NULL, 0, ...)
and remove all of the strlen
lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed buf and strlen wherever possible by using len = snprintf(NULL, 0, ...)
. Used malloc() instead of stack memory.
usr.bin/sockstat/sockstat.c
Outdated
/* Remote peer we connect(2) to, if any. */ | ||
if (faddr->conn != 0) { | ||
struct sock *p; | ||
strcpy(buf, "-> "); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strcpy
should almost always be avoided. Use strncpy
, or better yet strlcpy
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced strcpy
with strlcpy
.
9761665
to
7c8ce27
Compare
Refactor sockstat to dynamically size table columns based on content. This eliminates the need for the -w option, which is now ignored for backwards compatibility. Unknown fields are now consistently shown as "??" instead of a mix of "", "?" and "?" for output uniformity. Sponsored by: Google, LLC (GSoC 2025) MFC after: 2 weeks
7c8ce27
to
c0df8c6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pro tips:
- Once a reviewer first looks at a PR, never force-push it. If you must amend
it, leave your changes in a separate commit. That makes it easy for the
reviewer to see what's changed since his first review. - Never merge the main branch into your PR branch. That can confuse the
history and doesn't really solve any problems. Instead, better to leave the
PR out-of-date unless it conflicts with the main branch. If you must resolve
conflicts, it's better to rebase than to merge.
I'll come back tomorrow for a lengthier review. I'll have to re-review the entire thing.
Thanks for the advice, @asomers. I will make sure to follow this workflow and keep changes in separate commits after reviews begin. |
Refactor sockstat to dynamically size table columns based on content.
This eliminates the need for the -w option, which is now ignored
for backwards compatibility.
Unknown fields are now consistently shown as "??" instead of a mix
of "", "?" and "?" for output uniformity.
Sponsored by: Google, LLC (GSoC 2025)
MFC after: 2 weeks